package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.comment.domain.SceneryComment;
import cn.wolfcode.luowowo.comment.domain.ScenicRepComment;
import cn.wolfcode.luowowo.comment.query.SceneryCommentQuery;
import cn.wolfcode.luowowo.comment.service.ISceneryCommentService;
import cn.wolfcode.luowowo.comment.service.IScenicRepCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    @Reference
    private ISceneryService sceneryService;

    @Reference
    private ISceneryCommentService sceneryCommentService;

    @Reference
    private IScenicRepCommentService scenicRepCommentService;

    @RequestMapping("")
    public String index(Model model){
        //热门区域
        List<Region> regions = regionService.queryHotRegions();
        model.addAttribute("hotRegions",regions);
        return "destination/index";
    }

    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model, Long regionId){
        //通过区域id查询关联目的地
        List<Destination> destinations = destinationService.getDestByRegionId(regionId);
        //分为左右两边的list
        List<Destination> leftDests = new ArrayList<>();
        List<Destination> rightDests = new ArrayList<>();
        if (destinations != null && destinations.size() > 0){
//            int mid = destinations.size() / 2;
            for (int i = 0; i < destinations.size(); i++) {
                if (i % 2 == 0){
                    leftDests.add(destinations.get(i));
                }else {
                    rightDests.add(destinations.get(i));
                }
            }
        }
        //把左右两个目的地列表共享到页面
        model.addAttribute("leftDests", leftDests);
        model.addAttribute("rightDests", rightDests);
        model.addAttribute("regionId", regionId);
        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public String guide(Model model, Long id){
        //面包屑数据
        List<Destination> toasts = destinationService.getToasts(id);
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        //目的地数据
        model.addAttribute("dest", destination);
        //攻略分类
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(id);
        model.addAttribute("catalogs", catalogs);

        //strategyDetails
        List<StrategyDetail> details = strategyDetailService.getViewTop3(id);
        model.addAttribute("strategyDetails", details);
        return "destination/guide";
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        //dest
        model.addAttribute("dest", destination);
        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {
        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(qo.getDestId());
        StrategyCatalog catalog = null;
        if (catalogs != null && catalogs.size() > 0) {
            for (StrategyCatalog c : catalogs) {
                if (c.getId() == qo.getCatalogId()) {
                    //页面选中分类
                    catalog = c;
                    model.addAttribute("catalog", c);
                }
            }
        }
        model.addAttribute("catalogs", catalogs);
        //detail
        if (catalog != null){
            List<StrategyDetail> details = catalog.getDetails();
            if (details != null && details.size() > 0) {
                StrategyDetail detail = details.get(0);
                StrategyContent content = strategyDetailService.getContent(detail.getId());
                detail.setStrategyContent(content);
                model.addAttribute("detail", detail);
            }
        }
        return "destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo") TravelQuery qo) {
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "destination/travelTpl";
    }


    @RequestMapping("/scenery")
    public String scenery(Model model,Long id) {
        //面包屑数据
        List<Destination> toasts = destinationService.getToasts(id);
        Destination destination = toasts.remove(toasts.size() - 1);
        toasts.remove(0);
        model.addAttribute("toasts", toasts);
        //目的地数据
        model.addAttribute("destination", destination);
        List<Scenery> list = sceneryService.getTop5(id);
        model.addAttribute("list", list);
        Destination dest = destinationService.getDestById(id);
        model.addAttribute("dest", dest);
        List<Scenery> hotList = sceneryService.getHotScenery(id);
        model.addAttribute("hotList", hotList);
        return "destination/scenery";
    }

    @RequestMapping("/detail")
    public String detail(Model model,@ModelAttribute("qo") SceneryCommentQuery qo,@UserParam UserInfo user) {
        Scenery scenery = sceneryService.getSceneryById(qo.getSceneryId());
        model.addAttribute("scenery",scenery);
        Page page = sceneryCommentService.query(qo);
        List<SceneryComment> content = page.getContent();
        for (SceneryComment sceneryComment : content) {
            List<ScenicRepComment> repComments = scenicRepCommentService.queryByCommentId(sceneryComment.getId()).getContent();
            if(repComments != null){
                sceneryComment.setRepComments(repComments);
            }
        }
        model.addAttribute("page", page);
        model.addAttribute("content", content);
        return "destination/detail";
    }



    //评论操作
    @RequestMapping("/commentAdd")
    @ResponseBody
    public AjaxResult commentAdd(SceneryComment comment, @UserParam UserInfo user,String[] coverUrl) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < coverUrl.length; i++) {
            if(i==coverUrl.length-1){
                sb.append(coverUrl[i]);
            }else {
                sb.append(coverUrl[i]).append(",");
            }
        }
        comment.setImgList(sb.toString());
        comment.setHeadUrl(user.getHeadImgUrl());
        comment.setLevel(user.getLevel());
        comment.setUserId(user.getId());
        comment.setUsername(user.getNickname());
        System.out.println(comment.toString());
        //保存评论
        sceneryCommentService.save(comment);
        return AjaxResult.SUCCESS;//评论数据
    }

    @RequestMapping("comment")
    public String comment(Model model,@UserParam UserInfo userInfo, ScenicRepComment repComment){

        repComment.setUsername(userInfo.getNickname());
        repComment.setHeadUrl(userInfo.getHeadImgUrl());
        repComment.setUserId(userInfo.getId());
        repComment.setLevel(userInfo.getLevel());
        ScenicRepComment scenicRepComment = scenicRepCommentService.save(repComment);
        model.addAttribute("r", scenicRepComment);
        return  "destination/commentTpl";
    }
}
